package com.lipi.mall.product.app;

import com.lipi.common.utils.PageUtils;
import com.lipi.common.utils.R;
import com.lipi.mall.product.entity.ProductAttrValueEntity;
import com.lipi.mall.product.service.AttrService;
import com.lipi.mall.product.service.ProductAttrValueService;
import com.lipi.mall.product.vo.AttrRespVO;
import com.lipi.mall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author hecy
 * @email kuanih200@163.com
 * @date 2021-06-14 16:02:24
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * /product/attr/base/listforspu/{spuId}
     */
    @GetMapping("/{attrType}/listforspu/{spuId}")
    public R baseAttrList(@PathVariable("spuId") Long spuId) {

        List<ProductAttrValueEntity> data = productAttrValueService.baseAttrlistForspu(spuId);
        return R.ok().put("data", data);
    }

    /**
     * /product/attr/update/{spuId}
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities) {

        productAttrValueService.updateSpuAttr(spuId, entities);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     * {attrType}:操作类型：【base】获取基本属性，【sale】获取销售属性
     */
    // product/attr/base/list/0?
    // product/attr/sale/list/0?
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params
            , @PathVariable("catelogId") Long categoryId
            , @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.queryPage(params, categoryId, attrType);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVO attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attrVO) {
        attrService.updateByAttr(attrVO);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
